package chkui.springcore.example.javabase.conversion.support;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import chkui.springcore.example.javabase.conversion.entity.Device;

public class String2DeviceConverterFactory implements ConverterFactory<String, Device> {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public <T extends Device> Converter<String, T> getConverter(Class<T> targetType) {
		return new String2DeviceConverter(targetType);
	}

	static class String2DeviceConverter<T extends Device> implements Converter<String, Device> {

		private Class<? extends Device> klass;

		public String2DeviceConverter(Class<? extends Device> klass) {
			this.klass = klass;
		}

		@SuppressWarnings("unchecked")
		@Override
		public T convert(String source) {
			Device device = null;
			try {
				device = klass.newInstance();
				device.pares(source);
			} catch (InstantiationException | IllegalAccessException e) {
				e.printStackTrace();
			}
			return (T) device;
		}
	}
}
